home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_09_06
/
9n06106a
< prev
next >
Wrap
Text File
|
1990-12-10
|
1KB
|
42 lines
//wcount.cpp
//counts lines, words, non-whitespace characters and bytes in
//ASCII files
#include <stdio.h>
#include <stream.hpp>
#include <stdlib.h>
#include <ctype.h>
main(int argc, char *argv[])
{
unsigned char ch, lastch;
unsigned long bytecount = 0, charcount = 0,
wordcount = 0, linecount = 0;
filebuf file1;
if (file1.open(argv[1],input) == 0) exit(1);
istream input_file(&file1);
for (;;){
input_file.get(ch);
if (ch == '\n') linecount++;
if (bytecount == 0 && !isgraph(ch)) lastch = ch;
if (!isspace(lastch) && !iscntrl(lastch) && (isspace(ch)
|| iscntrl(ch) || input_file.eof())) wordcount++;
if (input_file.eof()) break;
if (!isspace(ch) && !iscntrl(ch)) charcount++;
bytecount++;
lastch = ch;
}
if (linecount == 0) linecount = 1;
cout << "File " << argv[1] << " contains:\n"
<< dec(linecount,21) << " line(s)\n"
<< dec(wordcount,21) << " words\n" << dec(charcount,21)
<< " characters excluding whitespace\n"
<< dec(bytecount,21) << " bytes\n";
}